<?

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
#	FILE:			includes/custom/adminpages.php
#	FUNCTION:		Custom handler for the Admin Pages page
#	AUTHOR:			Cameron Morrow
#	CREATED:		02/08/2005
#
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# D A T A   F O R   U S E R S
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# Table
$current_table = $PROJECT_DB_TABLES["pages"];

# Field to sort browse on
$browse_sort_field = "p_id";
$browse_sort_field = "p_name";

# Field to group browse on
$browse_group_field = "p_sort";

# Sort type
$browse_group_sort_type = "ASC";

# Is index numeric
$index_numeric = false;

# Index field
$index_field = "p_id";

# Type name
$type_name = "Page";

# Plural
$type_name_plural = "Pages";

# Custom Columns
$custom_columns = array();

# Add Move column
$custom_columns[] =array("Move", "\"<a href=\\\"javascript:launchPageMove('[p_id]');\\\">Move</a>\"");

# Add Preview column
$custom_columns[] = array("Preview", "\"<a href=\\\"javascript:launchPagePreview('[p_id]');\\\">Preview</a>\"");

# Add Edit column
$custom_columns[] =array("Content", "\"<a href=\\\"javascript:editPageContent('[p_id]');\\\">Content</a>\"");

# Actions
$custom_columns[] = generateAddEditColumn($index_field);

# Browse SQL
$browse_sql_additional_where_clauses = array("p_section != '" . $NEWSLETTER_DIR_NAME . "'");

# Hide locked items if user can't edit them
if ((integer) $USER -> getCanLock() != 1) {
	$browse_sql_additional_where_clauses[] = "p_status != 2";
}

$browse_sql_search_fields = array("p_name", "p_id");

# Edit SQL
$edit_sql_additional_selects = array();
$edit_sql_additional_tables = array();
$edit_sql_additional_where_clauses = array();

if(@$_POST["sort"]){
	$current_sort_parent=$_POST["mydropdown"];
	$browse_sql_item="SELECT * from ".$PROJECT_DB_TABLES['pages']." WHERE p_parent=\"".$current_sort_parent."\" order by p_id;";
	$current_item=getRecord($browse_sql_item);
	$index=1;
	foreach($current_item as $current_item_key=>$current_item_value){
		$browse_sql_update="UPDATE ".$PROJECT_DB_TABLES['pages']." SET p_sort=".$index." where p_id=\"".$current_item_value["p_id"]."\"";
		//echo "browse_sql_update:".$browse_sql_update."<br/>";
		executeSQLQuery($browse_sql_update);
		$index=$index+1;
	}
}
if(@$_POST["reorganize"]){
	$current_reorganize=$_POST["mydropdown1"];
	$current_status=$_POST["mydropdown2"];
	$browse_sql_reorganize="SELECT * from ".$PROJECT_DB_TABLES['pages']." WHERE p_parent=\"".$current_reorganize."\" order by p_id;";
	$current_sql_reorganize=getRecord($browse_sql_reorganize);
	
	//foreach($current_sql_reorganize as $current_sql_reorganize_key=>$current_sql_reorganize_value){
    if($current_status=="add"){
		for($i=count($current_sql_reorganize)-1;$i>=0;$i--){
			$pieces=explode("_",$current_sql_reorganize[$i]["p_id"]);
			$value=intval($pieces[2]);
			$value++;
			if($value>=10){
				$pieces[2]=$value;
			}
			else{
				$pieces[2]="0".$value;
			}
			$result=implode("_",$pieces);
			$browse_sql_reorganize="UPDATE ".$PROJECT_DB_TABLES['pages']." SET p_id=\"".$result."\" where p_id=\"".$current_sql_reorganize[$i]["p_id"]."\"";
			//echo $browse_sql_reorganize;
			executeSQLQuery($browse_sql_reorganize);
		}	
	}
	else{
		for($i=0;$i<count($current_sql_reorganize);$i++){
			$pieces=explode("_",$current_sql_reorganize[$i]["p_id"]);
			$value=intval($pieces[2]);
			$value--;
			if($value<10){
				$pieces[2]="0".$value;
			}
			else{
				$pieces[2]=$value;
			}
			$result=implode("_",$pieces);
			$browse_sql_reorganize="UPDATE ".$PROJECT_DB_TABLES['pages']." SET p_id=\"".$result."\" where p_id=\"".$current_sql_reorganize[$i]["p_id"]."\"";
			//echo $browse_sql_reorganize;
			executeSQLQuery($browse_sql_reorganize);
		}
	}
}

include("../includes/custom/adminhandler.php");
?>